enum TypeEnum {
    wave,
}
var Type = cc.Enum(TypeEnum);

const { ccclass, property } = cc._decorator;

@ccclass
export default class clickAction extends cc.Component {

    @property({
        type: Type,
        tooltip: 'wave: 从点击处向外扩散一个圆'
    })
    type = Type.wave;
    @property(cc.Float)
    duration: number = 0.3;
    // @property({
    //     type: cc.Boolean,
    //     tooltip: '是否阻挡下层点击事件'
    // })
    // isBlockInput: boolean = false;

    // LIFE-CYCLE CALLBACKS:
    private picNode: cc.Node = null;

    onLoad() {
        this.picNode = this.node.getChildByName('pic');
        this.hide();
        this.scheduleOnce(this.resetWH.bind(this));
        this.onButtonEvent();
    }

    start() {

    }

    // update (dt) {}
    private hide() {
        this.picNode.opacity = 0;
    }
    private show() {
        this.resetNode();
    }
    private resetNode() {
        this.picNode.width = this.node.width;
        this.picNode.height = this.node.height;
        this.picNode.setPosition(cc.v2());
        this.picNode.angle = 0;
        this.picNode.setScale(1);
        this.picNode.setAnchorPoint(cc.v2(0.5, 0.5));
        // this.picNode.color
        this.picNode.opacity = 255;
        this.picNode.skewX = 0;
        this.picNode.skewY = 0;
    }
    private resetWH() {
        let w = this.picNode.width = this.node.width = this.node.parent.width;
        let h = this.picNode.height = this.node.height = this.node.parent.height;
        w < h ? h = w : w = h;
        this.picNode.width = w;
        this.picNode.height = h;
    }
    private onButtonEvent() {
        let btn = this.node.getComponent(cc.Button);
        btn || (btn = this.node.addComponent(cc.Button));
        let event = new cc.Component.EventHandler();
        event.component = 'clickAction';
        event.handler = 'onClickBtn';
        event.target = btn.node;
        btn.clickEvents.push(event);
    }
    private getAction() {
        switch (this.type) {
            case TypeEnum.wave: {
                this.picNode.scale = 0;
                let st = cc.scaleTo(this.duration, 5);
                let fo = cc.fadeOut(this.duration);
                let spw = cc.spawn(st, fo);
                let ck = cc.callFunc(function () {
                    this.hide();
                }.bind(this));
                return cc.sequence(spw, ck);
            } break;
        }
    }
    private runAction() {
        this.picNode.stopAllActions();
        let act = this.getAction();
        this.picNode.runAction(act);
    }
    private emitEvent() {
        // if (this.isBlockInput) return;
        let btn = this.node.parent.getComponent(cc.Button);
        cc.Component.EventHandler.emitEvents(btn.clickEvents);
    }
    onClickBtn(evt) {
        this.show();
        let location = evt.touch.getLocation();
        let w_pos = this.picNode.convertToWorldSpaceAR(cc.v2());
        let offset = location.sub(w_pos);
        this.picNode.x += offset.x;
        this.picNode.y += offset.y;
        this.runAction();

        this.emitEvent();
    }
}
